package com.baseframe.score.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.score.entity.SnsScoreRules;
import com.baseframe.score.entity.request.SnsScoreRuleRequest;
import com.baseframe.score.entity.vo.SnsScoreRulesVO;
import com.baseframe.score.service.SnsScoreRulesService;
import com.baseframe.tool.result.R;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员积分规则控制器
 */
@RestController
@RequestMapping("/back/score/rule")
@AllArgsConstructor
public class BackSnsScoreRuleController {

    /**
     * 注入积分规则服务service
     */
    private SnsScoreRulesService snsScoreRulesService;

    /**
     * 更新积分规则
     * @param request
     * @return
     */
    @PostMapping("/update")
    public R<Object> update(@RequestBody SnsScoreRuleRequest request){
        /**
         * 判断必要参数是否存在
         */
        if (request.getRewardItem()==null){
            return R.fail("缺少必要请求参数");
        }
        /**
         * 判断奖励值是否合法, 并且更新积分值
         */
        return snsScoreRulesService.update(new LambdaUpdateWrapper<SnsScoreRules>()
                .eq(SnsScoreRules::getRewardItem,request.getRewardItem())
                .set(SnsScoreRules::getRewardValue,request.getRewardValue()))
                ?
                R.success():R.fail()
                ;
    }

    /**
     * 分页展示所有积分规则
     * @param pageInfo
     * @param request
     * @return
     */
    @GetMapping("/page")
    public R<Page<SnsScoreRulesVO>> snsScorePageList(PageInfo pageInfo, SnsScoreRuleRequest request){

        return R.data(snsScoreRulesService.snsScorePageList(pageInfo,request));
    }


}
